//
//  HLAccountLoginService.swift
//  HLXiMaLaYa
//
//  Created by hulinhu on 2018/2/27.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class HLAccountLoginService {
    
    static let shareInstance = HLAccountLoginService()
    private init() {}
    
    // 验证账号是否合法
    func validationAccount(_ account: String) -> Observable<HLAccountLoginResult> {
        
        if (account =~ isIphoneNum) == false {
            return Observable.just(HLAccountLoginResult.failed(message: "账号非法"))
        }
        return Observable.just(HLAccountLoginResult.ok(message: "账号合法"))
    }
    
    // 验证密码是否合法
    func validationPassword(_ passsword: String) -> Observable<HLAccountLoginResult> {
        
        if (passsword =~ Password) == false {
            return Observable.just(HLAccountLoginResult.failed(message: "密码非法"))
        }
        return Observable.just(HLAccountLoginResult.ok(message: "密码合法"))
    }
    
    // 登录请求
    func login(account: String, password: String) -> Observable<HLAccountLoginResult> {
        
        if account.characters.count > 10 {
            return Observable.just(HLAccountLoginResult.ok(message: "登录成功"))
        } else {
            return Observable.just(HLAccountLoginResult.failed(message: "密码错误"))
        }
    }
    
    // 登录按钮是否可用
    func loginBtnEnable(account: String, password: String) -> Observable<Bool> {
        
        if ((account =~ isIphoneNum) && (password =~ Password)) {
            return Observable.just(true)
        } else {
            return Observable.just(false)
        }
    }
}

